/**
 * Copyright (c) 2015-2016, smeooncun 失色 (semooncun@foxmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lubiaoqing.jsite.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lubiaoqing.jsite.JSiteConsts;
import com.lubiaoqing.jsite.util.AdminMenu;
import com.jfinal.handler.Handler;

public class JHandler extends Handler {

	@Override
	public void handle(String target, HttpServletRequest request,
			HttpServletResponse response, boolean[] isHandled) {

		// 设置模板参数
		String CPATH = request.getContextPath();
		request.setAttribute("REQUEST", request);
		request.setAttribute("CPATH", CPATH);
		request.setAttribute("SPATH", CPATH + "/static");
		request.setAttribute("JSITE_VERSION", JSiteConsts.VERSION);
		// 设置网站参数
		
		//伪静态
		int index = target.lastIndexOf(".html");
		if (index != -1) {
			target = target.substring(0, index);
		}
		
		// 配置菜单
		if (!(target.endsWith(".ico") || target.endsWith(".png")
				|| target.endsWith(".jpg") || target.endsWith(".js")
				|| target.endsWith(".css") || target.endsWith(".jpg"))) {
			if (target.startsWith("/admin")) {
				String[] a = target.split("/");
				if (a.length > 2) {
					AdminMenu.setActive(a[2]);
				} else {
					AdminMenu.setActive("index");
				}
				request.setAttribute("AMDIN_MENU", AdminMenu.generateHtml(CPATH));
			} else {
				// 前端导航
				
			}
		}

		next.handle(target, request, response, isHandled);
	}

}
